package com.tangcco.lockwell.utility;

import com.fasterxml.jackson.databind.JsonNode;
import com.tangcco.lockwell.config.RestTemplateConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 调用ai
 *
 * @author yanxinping
 * @date 2024-12-31 14:13
 */
@Configuration
@Slf4j
public class AiUtil {
    @Resource
    private RestTemplate aiClient;

    public JsonNode qiReplyQuestion(String questionContent) {
        try {
            String url = "/qa";
            log.info("ai答疑，请求参数：{}", questionContent);
            JsonNode jsonNode =  aiClient.postForObject(RestTemplateConfig.aiUrl + url, questionContent, JsonNode.class);
            log.info("ai答疑，返回结果：{}",jsonNode);
            return jsonNode;
        } catch (Exception e) {
            log.error("AI答疑请求失败: ", e);
            throw e;
        }
    }
    public JsonNode politicsAnalyzeQuestion(String question, String answer) {
        try {
            String url = "/politics/analyze";
            log.info("题目解析，请求参数：{},{}", question, answer);
            Map<String, String> map = new HashMap<>();
            map.put("question", question);
            map.put("answer", answer);
            JsonNode jsonNode =  aiClient.postForObject(RestTemplateConfig.aiUrl + url, map, JsonNode.class);
            log.info("ai题目解析，返回结果：{}",jsonNode);
            return jsonNode;
        } catch (Exception e) {
            log.error("AI题目解析请求失败: ", e);
            throw e;
        }
    }
}

